package com.econ.powercloud.ems.service.dao.clickhouse;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.clickhouse.entity.EmsWarnRecord;
import com.econ.powercloud.clickhouse.mapper.EmsWarnRecordMapper;
import com.econ.powercloud.common.base.facade.util.TimeUtil;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Repository;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jwc
 * @date 2022/12/02
 */
@Repository
@DS("clickhouse")
public class EmsWarnRecordDAO extends ServiceImpl<EmsWarnRecordMapper, EmsWarnRecord> {

    public void insertBatch(String tableSuffix, List<EmsWarnRecord> list) {
        this.getBaseMapper().insertBatch(tableSuffix, list);
    }

    public List<EmsWarnRecord> selectByPidsAndTime(List<String> pids, Boolean isAsc, Long startTime, Long endTime) {
        List<String> timeList = TimeUtil.getMonthDateBetweenTimestamp(startTime, endTime);
        List<EmsWarnRecord> result = Lists.newArrayList();
        timeList.stream().forEach(d -> {
            List<EmsWarnRecord> analogDataList = this.baseMapper.selectByTimeInPid(d, startTime, endTime, pids);
            if (CollectionUtils.isNotEmpty(analogDataList)) {
                result.addAll(analogDataList);
            }
        });
        return result.stream().sorted(isAsc ? Comparator.comparing(EmsWarnRecord::getWarnTime) : Comparator.comparing(EmsWarnRecord::getWarnTime).reversed())
                .collect(Collectors.toList());
    }

    public int updateById(String tableSuffix, EmsWarnRecord record) {
        return this.getBaseMapper().updateById(tableSuffix, record);
    }

}
